package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-store/cache"
	"logi-store/models"
	"logi-store/pkj"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	redis_key := "login_err_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误超过三次禁止登录",
		})
		return
	}
	var user models.User
	err := models.DB.Where("username=?", username).Debug().Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户查找失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if user.Password != pkj.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误三次,请两小时后重试",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": user,
		"msg":  "登录成功",
	})
	return
}

func RoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	if rid == 0 || nid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	models.DB.Debug().Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "分配成功",
	})
	return
}

/*
SELECT

	*

FROM

	roles
	LEFT JOIN role_nodes ON role_nodes.rid=roles.id
*/
func UserRole(c *gin.Context) {
	uid := c.Query("uid")
	var list models.Role
	models.DB.Table("roles").Joins("LEFT JOIN user_roles ON user_roles.rid=roles.id").Where("user_roles.uid=?", uid).Debug().Find(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "查询成功",
	})
	return
}
func RoleNodeList(c *gin.Context) {
	nid := c.Query("nid")
	var list models.Node
	models.DB.Table("nodes").Joins("LEFT JOIN role_nodes ON role_nodes.nid=nodes.id").Where("role_nodes.nid=?", nid).Debug().Find(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "查询成功",
	})
	return
}

func Good(c *gin.Context) {
	good_name := c.PostForm("good_name")
	if good_name == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Good{
		GoodName: good_name,
	}
	models.DB.Debug().Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderGood(c *gin.Context) {
	gid, _ := strconv.Atoi(c.PostForm("gid"))
	order_name := c.PostForm("order_name")
	order_type := c.PostForm("order_type")
	if gid == 0 || order_name == "" || order_type == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.OrderGood{
		Gid:       gid,
		OrderName: order_name,
		OrderType: order_type,
	}
	models.DB.Debug().Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

/*
SELECT

	order_goods.gid as Gid,
	order_goods.order_name as OrderName,
	order_goods.order_type as OrderType,
	goods.good_name as GoodName

FROM

	order_goods
	LEFT JOIN goods ON goods.id = order_goods.gid
*/
func ListOrderGood(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	redis_key := "list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	var list []models.ListOrder
	if cacheData == "" {
		models.DB.Table("order_goods").Select("order_goods.gid as Gid",
			"order_goods.order_name as OrderName",
			"order_goods.order_type as OrderType",
			"goods.good_name as GoodName").Joins("LEFT JOIN goods ON goods.id=order_goods.gid").Offset(offset).Limit(size).Debug().Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var total int64
	models.DB.Table("order_goods").Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"data":  list,
		"msg":   "列表成功",
		"total": total,
	})
	return
}
